﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dac.DAL.Repositories;
using Dac.Entities;

namespace Dac
{
    public partial class Default : System.Web.UI.Page
    {
        private string Lang
        {
            get
            {
                string lang = string.Empty;
                if (!string.IsNullOrEmpty(Request.QueryString["lang"]))
                    lang = Request.QueryString["lang"];
                return lang;
            }
        }
        private string ProductPathUrl
        {
            get
            {
                return ResolveClientUrl("~/Files/Products/");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<ProductRepository>())
            {
                repeater.DataSource = repo.FindAll();
                repeater.DataBind();
            }
        }

        protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var good = e.Item.DataItem as Product;
                Image img = e.Item.FindControl("imgPicture") as Image;
                HyperLink linkImage = e.Item.FindControl("linkImage") as HyperLink;
                HyperLink linkTitle = e.Item.FindControl("linkTitle") as HyperLink;
                Label lbTitle = e.Item.FindControl("lbTitle") as Label;

                if (good != null && Lang==string.Empty)
                {
                    img.ImageUrl = ProductPathUrl + good.ProductId.ToString() + "/" + good.PreviewImage;
                    linkImage.NavigateUrl = "ProductDetail.aspx?id=" + good.ProductId;
                    linkTitle.NavigateUrl = "ProductDetail.aspx?id=" + good.ProductId;
                    lbTitle.Text = good.ProductNameRu;
                }
                else if (good != null && Lang == "eng")
                {
                    img.ImageUrl = ProductPathUrl + good.ProductId.ToString() + "/" + good.PreviewImage;
                    linkImage.NavigateUrl = "ProductDetail.aspx?lang=eng&id=" + good.ProductId;
                    linkTitle.NavigateUrl = "ProductDetail.aspx?lang=eng&id=" + good.ProductId;
                    lbTitle.Text = good.ProductNameEn;
                }

            }
        }
    }
}